﻿using BLL;
using DAL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Data.Linq;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Security.Cryptography;

public partial class admincp_Employees : System.Web.UI.Page
{
    DigitalWorldDataContext db = new DigitalWorldDataContext();
    EmployeesBLL emp = new EmployeesBLL();
    CustomersBLL cus = new CustomersBLL();
    employee em = new employee();
    customer c = new customer();
    position p = new position();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string vitri = Page.RouteData.Values["ViTri"].ToString();
            
            hlEmployees.NavigateUrl = "~/Quan-Ly-Nhan-Vien-Khach-Hang/" + vitri + "/" + 0;
            hlCustomers.NavigateUrl = "~/Quan-Ly-Nhan-Vien-Khach-Hang/" + vitri + "/" + 1;
            string id = Page.RouteData.Values["id"].ToString();
            if (id == "1")
            {
                MutiView1.ActiveViewIndex = 1;
                this.loadData_Customer();
                rbtnStatus_Customer.Enabled = false;
                btnUpdate2.Enabled = false;
            }
            else
            {
                MutiView1.ActiveViewIndex = 0;
                loadPosition();
                this.LoadData_Employee();
                rbtnStatus.Enabled = false;
                btnUpdate.Enabled = false;
            }
            Session["EmployeeID"] = null;
            Session["CustomerID"] = null;
        }
    }
    #region LoadData_Employees_To_GridView
    /// <summary>
    /// Hàm lấy danh sách thông tin nhân viên lên gridview
    /// </summary>
    void LoadData_Employee()
    {
        gvEmployees.DataSource = emp.getEmployee();
        gvEmployees.DataBind();
    }
    #endregion

    #region LoadData_Customers_To_GridView
    /// <summary>
    /// Hàm lấy thông tin khách hàng lên gridview
    /// </summary>
    void loadData_Customer() {
        gvCustomers.DataSource = cus.getCustomer();
        gvCustomers.DataBind();
    }
    #endregion

    #region Load_Employees_To_TextBox
    /// <summary>
    /// Hàm load 1 dòng nhân viên lên panel thông tin
    /// </summary>
    void AcctionView1()
    {
        var EmpID = Session["EmployeeID"];
        var danhsach = db.employees.SingleOrDefault(em => em.employeeid.Equals(EmpID));
        ddlPositions.SelectedValue = danhsach.positionid.ToString();
        txtLastName.Text = danhsach.lastname.ToString();
        txtFirstName.Text = danhsach.firstname.ToString();
        txtAdminName.Text = danhsach.adminname.ToString();
        DateTime birthdate = DateTime.Parse(danhsach.birthdate.ToString());
        txtBirthDate.Text = DateTime.Parse(danhsach.birthdate.ToString()).ToString("MM/dd/yyyy");
        txtHireDate.Text = DateTime.Parse(danhsach.hiredate.ToString()).ToString("MM/dd/yyyy");
        txtIdentityCard.Text = danhsach.identitycard.ToString();
        txtAddress.Text = danhsach.address.ToString();
        txtCity.Text = danhsach.city.ToString();
        txtPostalCode.Text = danhsach.postalcode.ToString();
        txtCountry.Text = danhsach.country.ToString();
        txtHomePhone.Text = danhsach.homephone.ToString();
        //img....
        //Response.Write(danhsach.First().photopath.ToString());
        if (danhsach.status == true)
            rbtnStatus.SelectedIndex = 0;
        else
            rbtnStatus.SelectedIndex = 1;

    }
    #endregion

    #region Load_Customers_To_TextBox
    /// <summary>
    /// Hàm load 1 dòng khách hàng lên panel thông tin
    /// </summary>
    void AcctionView2()
    {
        int CusID = int.Parse(Session["CustomerID"].ToString());
        var danhsach = db.customers.SingleOrDefault(cus => cus.customerid.Equals(CusID));
        txtCustomerName.Text = danhsach.customername.ToString();
        txtUserName.Text = danhsach.username.ToString();
        txtAddress_Customer.Text = danhsach.address.ToString();
        txtSignUpDate.Text = DateTime.Parse(danhsach.signupdate.ToString()).ToString("MM/dd/yyyy");
        txtCity_Customer.Text = danhsach.city.ToString();
        txtPostalCode_Customer.Text = danhsach.postalcode.ToString();
        txtCountry_Customer.Text = danhsach.country.ToString();
        txtPhone.Text = danhsach.phone.ToString();
        txtEmail.Text = danhsach.email.ToString();
        if (danhsach.status == true)
            rbtnStatus_Customer.SelectedIndex = 0;
        else
            rbtnStatus_Customer.SelectedIndex = 1;
    }
    #endregion

    #region Load_Positions
    /// <summary>
    /// Hàm lấy thông tin chức vụ
    /// </summary>
    void loadPosition()
    {
        ddlPositions.DataSource = db.positions;
        ddlPositions.DataTextField = "positionname";
        ddlPositions.DataValueField = "positionid";
        ddlPositions.DataBind();
    }
    #endregion
    
    #region Reset_Employees
    /// <summary>
    /// Hàm xoá trắng trang thông tin nhân viên
    /// </summary>
    void Reset_View1()
    {
        ddlPositions.SelectedIndex = 0;
        txtLastName.Text = string.Empty;
        txtFirstName.Text = string.Empty;
        txtAdminName.Text = string.Empty;
        txtPassword.Text = string.Empty;
        txtBirthDate.Text = string.Empty;
        txtHireDate.Text = string.Empty;
        txtIdentityCard.Text = string.Empty;
        txtAddress.Text = string.Empty;
        txtCity.Text = string.Empty;
        txtPostalCode.Text = string.Empty;
        txtCountry.Text = string.Empty;
        txtHomePhone.Text = string.Empty;
        txtLastName.Focus();
        rbtnStatus.Enabled = false;
        btnUpdate.Enabled = false;
    }
    #endregion

    #region Reset_Customers
    /// <summary>
    /// Hàm xoá trắng trang thông tin khách hàng
    /// </summary>
    void Reset_View2()
    {
        txtCustomerName.Text = string.Empty;
        txtUserName.Text = string.Empty;
        txtPassword_Customer.Text = string.Empty;
        txtAddress_Customer.Text = string.Empty;
        txtSignUpDate.Text = string.Empty;
        txtCity_Customer.Text = string.Empty;
        txtPostalCode_Customer.Text = string.Empty;
        txtCountry_Customer.Text = string.Empty;
        txtPhone.Text = string.Empty;
        txtEmail.Text = string.Empty;
        rbtnStatus_Customer.SelectedIndex = 0;
        txtCustomerName.Focus();
        rbtnStatus_Customer.Enabled = false;
        btnUpdate2.Enabled = false;
    }
    #endregion
    string MaHoa(string chuoiCanMaHoa) {
        byte[] data = Encoding.ASCII.GetBytes(chuoiCanMaHoa);
        SHA512 sha5 = new SHA512Managed();
        byte[] result = sha5.ComputeHash(data);
        string chuoiMaHoa = Encoding.ASCII.GetString(result);
        return chuoiMaHoa;
    }

    #region Set_Employees
    /// <summary>
    /// Hàm gán giá trị vào lớp nhân viên
    /// </summary>
    /// <param name="EmpID"></param>
    void SetEmployee(int EmpID)
    {
        
        em.employeeid = EmpID;
        em.positionid = int.Parse(ddlPositions.SelectedItem.Value);
        em.lastname = txtLastName.Text;
        em.firstname = txtFirstName.Text;
        em.adminname = txtAdminName.Text;
        em.password = MaHoa(txtPassword.Text);
        em.birthdate = DateTime.Parse(txtBirthDate.Text);
        em.hiredate = DateTime.Parse(txtHireDate.Text);
        em.identitycard = txtIdentityCard.Text;
        em.address = txtAddress.Text;
        em.city = txtCity.Text;
        em.postalcode = txtPostalCode.Text;
        em.country = txtCountry.Text;
        em.homephone = txtHomePhone.Text;
        //int tail1 = ful_Photo.FileName.LastIndexOf('.');
        //int tail2 = ful_Photo.FileName.Count();
        //string tail = ful_Photo.FileName.Substring(tail1, tail2);
        em.photopath = "";
        if (rbtnStatus.SelectedItem.Value == "1")
            em.status = true;
        else
            em.status = false;
    }
    #endregion

    #region Set_Customers
    /// <summary>
    /// Hàm gán giá trị vào lớp khách hàng
    /// </summary>
    /// <param name="CusID"></param>
    void SetCustomer(int CusID)
    {
        c.customerid = CusID;
        c.customername = txtCustomerName.Text;
        c.username = txtUserName.Text;
        c.password = MaHoa(txtPassword_Customer.Text);
        c.address = txtAddress_Customer.Text;
        c.signupdate = DateTime.Parse(txtSignUpDate.Text);
        c.city = txtCity_Customer.Text;
        c.postalcode = txtPostalCode_Customer.Text;
        c.country = txtCountry_Customer.Text;
        c.phone = txtPhone.Text;
        c.email = txtEmail.Text;
        if (rbtnStatus.SelectedItem.Value == "1")
            c.status = true;
        else
            c.status = false;
    }
    #endregion

    #region Method of Button
    protected void btnInsert_Click(object sender, EventArgs e)
    {
        try
        {
            SetEmployee(0);
            emp.Employee_Insert(em);
            //Response.Write("<script>alert('Thành công')</script>");
            LoadData_Employee();
            this.Reset_View1();
        }
        catch
        {
            //Response.Write("<script>alert('Thất bại')</script>");
        }
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        try
        {
            int EmpID = 4;
            SetEmployee(EmpID);
            emp.Employee_Update(em);
            Response.Write("<script>alert('Thành công')</script>");
            LoadData_Employee();
            this.Reset_View1();
        }
        catch
        {
            Response.Write("<script>alert('Thất bại')</script>");
        }
    }
    protected void btnInsert2_Click(object sender, EventArgs e)
    {
        try
        {
            SetCustomer(0);
            cus.Customer_Insert(c);
            this.loadData_Customer();
            Reset_View2();
        }
        catch
        {

        }
    }
    protected void btnUpdate2_Click(object sender, EventArgs e)
    {
        try
        {
            int CusID = int.Parse(Session["CustomerID"].ToString());
            SetCustomer(CusID);
            cus.Customer_Update(c);
            //Response.Write("<script>alert('Thành công')</script>");
            this.loadData_Customer();
            Reset_View2();
        }
        catch
        {
            //Response.Write("<script>alert('Thất bại')</script>");
        }
    }
    protected void gvEmployees_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        Session["EmployeeID"] = gvEmployees.Rows[e.NewSelectedIndex].Cells[0].Text;
        AcctionView1();
        rbtnStatus.Enabled = true;
        btnUpdate.Enabled = true;
    }
    protected void gvCustomers_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        Session["CustomerID"] = gvCustomers.Rows[e.NewSelectedIndex].Cells[0].Text;
        AcctionView2();
        rbtnStatus_Customer.Enabled = true;
        btnUpdate2.Enabled = true;
    }
    protected void gvEmployees_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvEmployees.PageIndex = e.NewPageIndex;
        this.LoadData_Employee();
    }
    protected void gvCustomers_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvCustomers.PageIndex = e.NewPageIndex;
        this.loadData_Customer();
    }
    protected void gvEmployees_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int EmpID = int.Parse(gvEmployees.Rows[e.RowIndex].Cells[0].Text);
        em.employeeid = EmpID;
        emp.Employee_Delete(em);
        this.LoadData_Employee();
    }
    protected void gvCustomers_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int CusID = int.Parse(gvCustomers.Rows[e.RowIndex].Cells[0].Text);
        c.customerid = CusID;
        cus.Customer_Delete(c);
        this.loadData_Customer();
    }
    protected void btnInsertBasic_Click(object sender, EventArgs e)
    {
        try
        {
            c.customername = txtCustomerName.Text;
            c.username = txtUserName.Text;
            c.password = txtPassword_Customer.Text;
            c.signupdate = DateTime.Now.Date;
            c.status = true;
            cus.Customer_BasicInsert(c);
            this.loadData_Customer();
            this.Reset_View2();
        }
        catch
        {
        }
    }
    #endregion

}